﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生选课及成绩查询管理系统
{
    public partial class fiindAllorOneClassInfo : Form
    {
        public fiindAllorOneClassInfo()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string classno = txt_clano.Text;
            if (string.IsNullOrEmpty(classno))
            {
                MessageBox.Show("请填入班级号查询");
                return;
            }
            string sql = "select * from class where classno =" + classno;
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql = "select * from class";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //string classno = txt_clano.Text;
            //if (string.IsNullOrEmpty(classno))
            //{
            //    MessageBox.Show("请填入课程号进行删除");
            //    return;
            //}
            //string sql = "delete from class where classno =" + classno;
            //int res = DBAccess.GetState(sql);
            //if (res != 0)
            //{
            //    MessageBox.Show("删除成功");
            //    button3_Click(sender,e);
            //}
            //else
            //{
            //    MessageBox.Show("删除失败");
            //    return;
            //}
        }

        private void fiindAllorOneClassInfo_Load(object sender, EventArgs e)
        {
            string sql = "select * from class";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string classno = txt_clano.Text;
            if (string.IsNullOrEmpty(classno))
            {
                MessageBox.Show("请填入班级号查询");
                return;
            }
            string sql = "select * from class where classno like '%" + classno+"%'";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            int res = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int j = 1; j <= 2; j++)
                {
                    string colo = this.dataGridView1.Columns[this.dataGridView1.Rows[i].Cells[j].ColumnIndex].HeaderText;
                    string sql = "update class set " + colo + "=@value where classno=@id";
                    SqlParameter[] pArr = { new SqlParameter("@value", dataGridView1.Rows[i].Cells[j].Value.ToString()), new SqlParameter("@id", dataGridView1.Rows[i].Cells[0].Value.ToString()) };
                    res = DBAccess.GetState(sql, pArr);
                }
            }
            if (res != 0)
            {
                MessageBox.Show("修改成功");
                fiindAllorOneClassInfo_Load(sender, e);
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridView1.CurrentCellAddress.Y + 1;
            int col = dataGridView1.CurrentCellAddress.X + 1;
            txt_clano.Text = dataGridView1.Rows[row - 1].Cells[0].Value.ToString();
        }
    }
}
